//
//  XTabBar.m
//  iAccountBook
//
//  Created by 侯伟 on 2021/9/23.
//

#import "XTabBar.h"

@implementation XTabBar

//- (instancetype)initWithFrame:(CGRect)frame{
//    if(self = [super initWithFrame:frame]){
//        [self addSubview:self.floatBtn];
//    }
//    return self;
//}
 //layoutSubviews遍历子控件寻找UITabBarButton，给UITabBarButton重新设置frame
- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat itemWidth = (SCREEN_WIDTH - 60)/2;
//    CGFloat x = 50;
    self.floatBtn.frame = CGRectMake((SCREEN_WIDTH - 60)/2, 2, 60, 44);
    
//    CGFloat space = (SCREEN_WIDTH - 50*2 - 2*50 - 52) / 2;
    
    CGFloat tabBarButtonIndex = 0;
    for (UIView *child in self.subviews) {
        Class class = NSClassFromString(@"UITabBarButton");
        if ([child isKindOfClass:class]) {
            // 重新设置frame
            CGRect frame = CGRectMake((itemWidth*tabBarButtonIndex) + (tabBarButtonIndex == 0?0:60), 0, itemWidth, 49);
            child.frame = frame;
            // 增加索引
//            if (tabBarButtonIndex == 0) {
//                tabBarButtonIndex++;
//            }
            tabBarButtonIndex++;
        }
    }
    
}

- (void)floatBtnAction:(UIButton *)sender{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"floatActionNotification" object:nil];
}


- (UIButton *)floatBtn {
    if (!_floatBtn) {
        UIButton* floatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [floatBtn setImage:[UIImage imageNamed:@"x_tabar_add"] forState:UIControlStateNormal];
        floatBtn.adjustsImageWhenHighlighted = false;
        [floatBtn addTarget:self action:@selector(floatBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:floatBtn];
        _floatBtn = floatBtn;
    }
    return _floatBtn;
}

@end
